/**
 * 充值页面
 * author:zhangjlt
 * date:2017年3月13日13:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var template = require("template");
	template.helper('addMoney', function(money,preferential) {
		if(money &&preferential) {
			var total = parseFloat(money)+parseFloat(preferential);
			return total;
		} else {
			return money
		}
	});
	var pay = {
		data: {},
		balance:"",
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {
			//返回
			$("#return_pay").unbind().on("click", function() {
				window.location.href = "#home";
			});
			$(document).off("click", '.pay_btn').on("click", '.pay_btn', function() {
				pay.getpay();
			});
			$(document).off("click", '.payon').on("click", '.payon', function() {
				$(".payon").removeClass("pay_active");
				$(this).addClass("pay_active");

			});
			$(document).off("click", '.payonway').on("click", '.payonway', function() {
				$(".payonway").removeClass("payway_active");
				$(this).addClass("payway_active");

			});
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#home").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: true,
					from: "left"
				});
			});
			Path.listen();
		},
		/**
		 * 页面数据加载
		 */
		initData: function() {
			var _this = this;
			var param = {
				"openId": window.localStorage.getItem("openId"),
				"appId": window.localStorage.getItem("appId"),

			};
			common.loadData({
				url: "/wxmember/service/getPayList",
				param: param,
				successCallBack: _this.getPayListCallBack, //回调函数
				dataSource: "getPayList",
				cache: false
			});

		},
		getpay: function() {
			var payway = $(".payway_active").attr("payway");
			var url = location.href.split('#')[0];
			url = url+"#home";
			pay.balance=$(".pay_active").find("span").text();
			var urlstr = encodeURIComponent(url);
			var param = {
				"openid": window.localStorage.getItem("openId"),
				"platformtype": "6",
				"balance":$(".pay_active").find("span").text(),
				"detialtype": "1",
				"bodytext": "weihuiyuanceshi",
				"appId": window.localStorage.getItem("appId"),
				"backTransUrl":urlstr
			};
			if(payway == "wechat") {
				common.loadData({
					url: "/wxmember/service/getPrePayId",
					param: param,
					successCallBack: pay.wechatPayCallBack, //回调函数
					dataSource: "getPayList"
				});

				//微信预支付
			} else if(payway == "union") {

				common.loadData({
					url: "/wxmember/service/unionPay",
					param: param,
					successCallBack: pay.unionPayCallBack, //回调函数
					dataSource: "getPayList"
				});
			}

		},
		wechatPayCallBack: function(res) {
			if(res.status == "0") {
				var paysignStr = res.data.paysign;
				if(paysignStr && paysignStr != "undefined") {
					var paysignObj = JSON.parse(paysignStr);
					var payurl = "http://ydyx.yonyou.com/weixin/wxpay.jsp";
					var appid = window.localStorage.getItem("appId");
					var openid = window.localStorage.getItem("openId");
					var timestamp = paysignObj.timestamp;
					var nonceStr = paysignObj.nonce_str;
					var prepay_id = paysignObj.package;
					var signType = paysignObj.signtype;
					var paySign = paysignObj.signature;
					var backurl = "http://ufwhy.yonyou.com:8096/index.html";
					backurl = encodeURIComponent(backurl);
					var amount = pay.balance;
					payurl += "?appid=" + appid + "&timestamp=" + timestamp + "&nonceStr=" + nonceStr + "&prepay_id=" + prepay_id;
					payurl += "&signType=" + signType + "&paySign=" + paySign + "&backurl=" + backurl + "&amount=" + amount + "&openid=" + openid;
					$("#wechat_pay").attr("action", payurl);
					$("#wechat_pay").submit();
				}

			}
		},
		unionPayCallBack: function(json) {
			var openid = window.localStorage.getItem("openId");
			var appid = window.localStorage.getItem("appId");
			console.log(JSON.stringify(json));
			if(json.status == "0") {
				if(json.action && json.action.length > 0) {
					$("#payId_pay").attr("action", json.action);

					var html = "";
					for(var key in json.data) {
						var value = json.data[key];
						html = '<input type="hidden" name="' + key + '" id="' + key + '"value="' + value + '">';
						$("#payId_pay").append(html);
					}
					$("#payId_pay").submit();
				}
			} else {
				common.showTip({
					content: json.message
				});
			}

		},
		//callback function
		getPayListCallBack: function(json) {
			console.log("调用后台ajax返回值为：" + JSON.stringify(json));
			if(json.status == "0") {
				if(json.data && json.data.list && json.data.list.length > 0) {
					pay.data = json;
					var html2 = template('payTemplate', pay.data);
					document.getElementById('main_pay').innerHTML = html2;
					var content=json.data.discountNote; 
					content=content.replace(/\r\n/g,"<br>")  
      				content=content.replace(/\n/g,"<br>");  
 
					$("#pay_discountNote").html(content);
					
					//iscroll滚动加入
					var iscroll = require("iscroll");
					common.initIscroll({
						container: "iscrollContent_payTo",
						showScrollBar: false
					});

				} else {
					var window_height = document.documentElement.clientHeight;
					$("#main_pay").height(window_height);
					$("#main_pay").css({ "margin-top": 0 });

					common.show_nodata("目前未开通充值业务，敬请期待", "main_pay");
				}
			} else {
				var window_height = document.documentElement.clientHeight;
				$("#main_pay").height(window_height);
				$("#main_pay").css({ "margin-top": 0 });

				common.show_nodata("目前未开通充值业务，敬请期待", "main_pay");
//				common.showTip({
//					content: json.message
//				});
			}

		},
		/**
		 * each page init mothed
		 */
		initialize: function() {
			//动态加载数据
			this.initData();
			//事件绑定
			this.bindEvent();
			//路由绑定
			this.bindRout();
		}
	};

	module.exports = pay;
});